<template>
  <div class="login">
    <header>
      <h2>登录</h2>
      <p>
        <i class="iconfont icon-gengduo"></i>
        <i class="iconfont icon-crm17"></i>
      </p>
    </header>
    <main>
      <h1>
        <img
          src="https://img2.baidu.com/it/u=4013105224,1674301217&fm=26&fmt=auto&gp=0.jpg"
          alt=""
        />
      </h1>
      <div class="con">
        <van-form @submit="onSubmit">
          <van-field
            v-model="username"
            name="username"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="password"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </main>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      username: "",
      password: "",
    }
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
      axios
        .post("/user/login", {
          username: this.username,
          password: this.password,
        })
        .then((res) => {
          console.log(res, "login");
          if (res.data.status === 400) {
            alert(res.data.description);
          } else {
            alert("登录成功");
            localStorage.setItem("username", this.username);
            window.localStorage.setItem('token',res.data.body.token)
            console.log(res.data.body.token)
            this.$router.push("/home/profile")
          }
        })
    },
  },
};
</script>

<style lang='scss' scoped>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  header {
    width: 100%;
    height: 55px;
    background-color: skyblue;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10px;
    color: #fff;
    h2 {
      font-weight: normal;
    }
    p {
      width: 80px;
      padding: 5px 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border: 1px solid #fff;
      border-radius: 30px;
    }
  }
}
main {
  flex: 1;
  overflow-y: scroll;
  margin-top: 10px;
  h1 {
    height: 100%;
    height: 200px;
    margin: 15px 0;
    img {
      width: 55%;
      height: 200px;
      margin: 0 100px;
    }
  }
  .con {
    width: 90%;
    height: 245px;
    border-bottom: 1px solid #eee;
    margin: 0 auto;
    li {
      width: 100%;
      height: 45px;
      margin: 30px 0;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      span {
        width: 100px;
        height: 100%;
        line-height: 45px;
        text-align: center;
        border: 1px solid #ddd;
        border-radius: 4px;
      }
      input {
        flex: 1;
        height: 45px;
        padding-left: 5px;
        border-radius: 5px;
        outline: none;
        border: 1px solid #ddd;
      }
      button {
        width: 100%;
        height: 45px;
        background-color: #58a8f3;
        outline: none;
        border: none;
        text-align: center;
        color: #fff;
        border-radius: 8px;
      }
      p {
        width: 100%;
        display: block;
      }
    }
  }
}
</style>